import { _decorator, Component, Node, find } from 'cc'
import { Label } from 'cc';
import { App } from '../../App';
import AbstractComponent from '../../base/AbstractComponent';
import CommConst from '../../const/CommConst';
import GameConst from '../../const/GameConst';
import wsTool from '../../net/ws';
import { PD } from '../../player/PlayerData';
import { LoadMg } from '../../tools/LoadMg';
import { loadCommonPreFab, showHallWallet, showTipBox } from '../../tools/PopuUp';
import UtilTool from '../../tools/UtilTool';
import { MultiRule } from '../multiplay/MultiRule';
import { BloodMatch } from './BloodMatch';
const { ccclass, property } = _decorator;

// 血流模式选择场次界面
@ccclass('BloodSelect')
export class BloodSelect extends AbstractComponent {

    @property(Node)
    node_primary : Node = null;

    @property(Node)
    node_high : Node = null;

    onLoad() {

    }

    start() {
        let anteLabelV1 = find("di/anteLabelV",this.node_primary).getComponent(Label)
        let limitLabel1 = find("di/limitLabel",this.node_primary).getComponent(Label)
        let sp_coin1 = find("di/sp_coin",this.node_primary)

        let anteLabelV2 = find("di/anteLabelV",this.node_high).getComponent(Label)
        let limitLabel2 = find("di/limitLabel",this.node_high).getComponent(Label)
        let sp_coin2 = find("di/sp_coin",this.node_high)

        // 底分
        anteLabelV1.string = PD.bloodConfig.apprenticeBaseScore + ""
        anteLabelV2.string = PD.bloodConfig.masterBaseScore + ""

        // 入场限制金额
        limitLabel1.string = PD.bloodConfig.apprenticeMinLimit + App.language.getLanguageLab(191)
        limitLabel2.string = PD.bloodConfig.masterMinLimit + App.language.getLanguageLab(191)

        // 适配位置
        UtilTool.adaptiveLabel([sp_coin1, limitLabel1.node],0 ,10)
        UtilTool.adaptiveLabel([sp_coin2, limitLabel2.node],0 ,10)
    }

    // 自动匹配
    autoMatch(bloodLevel) {
        let limitv = bloodLevel==0?PD.bloodConfig.apprenticeMinLimit: PD.bloodConfig.masterMinLimit
        if (PD.user.coin < limitv) {
        // if (true) {
            showTipBox({
                content: App.language.getLanguageLab(192),
                btnTexts1: App.language.getLanguageLab(193),
                btnType: 3,
                func:(btnIndex)=>{
                    if(btnIndex == 0) { // 确认
                        showHallWallet(null)
                    }
                }
                })

            return
        }

        this.openMatchPanel(bloodLevel)
    }

    // 初级场
    onBtnPrimary() {
        this.playClickAudio()
        this.autoMatch(0)
    }

    // 高级场
    onBtnHigh() {
        this.playClickAudio()
        this.autoMatch(1)
    }

    // 玩法说明
    async onBtnRule() {
        this.playClickAudio()
        let node = await loadCommonPreFab("prefabs/hall/multiplay/multiRule", null)
        node.getComponent(MultiRule).setGameType(2)

        // wsTool.send({msgCode:GameConst.Req_GIVEUP})
        // .then((data) => {
        //     if(data.result == 0) {
        //         UtilTool.printLog("认输成功")
        //     } else {
        //         UtilTool.printLog("认输失败", data.result)
        //     }
        // })
    }

    /**
     * 展示匹配界面
     */
    async openMatchPanel(bloodLevel) {
        let node = await loadCommonPreFab("prefabs/hall/bloodplay/MatchWnd")
        node.getComponent(BloodMatch).initData(bloodLevel)
    }

    // 返回
    onBtnBack() {
        this.playClickAudio()
        this.backToHall()
    }

}

